class DateUtil {
  static int YMD = 0;
  static int YM = 1;
  static int MD = 2;
  static int YMDHM = 3;
  static int HM = 4;
}

String getStrTime(DateTime? dt, [int? formatType]) {
  if (dt == null) {
    return "";
  }
  String y = "${dt.year}";
  String m = TwoDigits(dt.month);
  String d = TwoDigits(dt.day);
  String h = TwoDigits(dt.hour);
  String min = TwoDigits(dt.minute);
  String sec = TwoDigits(dt.second);
  if (formatType == DateUtil.YMD) {
    return "$y-$m-$d";
  } else if (formatType == DateUtil.YM) {
    return "$y-$m";
  } else if (formatType == DateUtil.MD) {
    return "$m-$d";
  } else if (formatType == DateUtil.YMDHM) {
    return "$y-$m-$d $h:$min";
  } else if (formatType == DateUtil.HM) {
    return "$h:$min";
  } else {
    return "$y-$m-$d $h:$min";
  }
}

String TwoDigits(int n) {
  if (n >= 10) return "$n";
  return "0$n";
}

/// 将utc0的time【String格式】转为本地DateTime
DateTime? getTimeByUtc(String? dateTime) {
  if (dateTime == null || dateTime.isEmpty) {
    return null;
  }
  DateTime time = DateTime.parse(dateTime).toLocal();
  return time;
}

/// 将time转为utc0的time【String格式】
String getUtcTime(DateTime dateTime) {
  String time = dateTime.toUtc().toString();
  time = time.replaceFirst(" ", "T");
  return time;
}

String getShortDate(String dateTime) {
  if (dateTime.length <= 10) {
    return dateTime;
  }
  String date = dateTime.substring(0, 10);
  return date;
}
